package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.model.entity.UserEntity;
import com.runen.infinitemovement.model.enums.ActivityCompat;
import com.runen.infinitemovement.tool.DataCleanManager;
import com.runen.infinitemovement.widget.popupwind.CommonDialog;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.SharePreferenceUtils;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on 2020/09/14 11:07
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_invite_friend)
    TextView tvInviteFriend;
    @BindView(R.id.tv_clean_cache)
    TextView tvCleanCache;
    @BindView(R.id.tv_about_app)
    TextView tvAboutApp;
    @BindView(R.id.tv_login_out)
    TextView tvLoginOut;
    @BindView(R.id.top_btn_right)
    Button topBtnRight;
    @BindView(R.id.top_iv_right)
    ImageView topIvRight;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("设置");
    }

    @OnClick({R.id.top_btn_left, R.id.tv_invite_friend, R.id.tv_clean_cache, R.id.tv_about_app, R.id.tv_login_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.tv_invite_friend:
//                Intent intent = new Intent(this, CheckJoinUsRecommendActivity.class);
//                jumpToInputTeamCode();
                checkUserChangeTopCompany();
                break;
            case R.id.tv_clean_cache:
                showDialogClean();
                break;
            case R.id.tv_about_app:
                jumpActivity(AboutAppActivity.class);
                break;
            case R.id.tv_login_out:
                outLogin();
                break;
        }
    }

    /**
     * 判断用户是否能切换一级军团
     */
    private void checkUserChangeTopCompany() {
        showDialog();
        new UserAPI().getWhetherSwitchCompany(new BaseResultCallback<BaseResponseEntity>() {
            @Override
            public void onResponse(BaseResponseEntity response) {
                dismissDialog();
                if (response != null && response.getCode() == 0) {
                    if (response.getData() != null && !TextUtils.isEmpty(response.getData().toString())) {
                        Intent intent = new Intent(SettingActivity.this, CheckTeamActivity.class);
                        intent.putExtra(ActivityCompat.TEAM_GROUP_ID, response.getData().toString());
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    } else {
                        jumpToInputTeamCode();
                    }
                } else {
                    jumpToInputTeamCode();
                }
            }
        });
    }

    private void jumpToInputTeamCode() {
        Intent intent = new Intent(SettingActivity.this, InputTeamCodeActivity.class);
        intent.putExtra("changeBranch", 1);
        startActivity(intent);
    }

    private void outLogin() {
        showDialog();
        String string = "?token=%s";
        new UserAPI().outLogin(String.format(string, UserAPI.getUserToken()), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                dismissDialog();
                if (response.getCode() != 0) {
                    ToastUtils.showShort(response.getMessage());
                } else {
                    UserAPI.clearLogin();
                    EventBus.getDefault().post(new EventBusEntity(7));
                    startActivity(new Intent(SettingActivity.this, LoginActivity2.class));
                    finish();
                }
            }
        });
    }

    private void showDialogClean() {
        UserEntity userEntity = UserAPI.getUserInfo();
        CommonDialog.getInstance(this, "清除缓存会清除所有的账户信息，是否要清除？", "确定")
                .setOnClickListener(new CommonDialog.OnClickListener() {
                    @Override
                    public void onSendListener() {
                        DataCleanManager.clearAllCache(SettingActivity.this);
                        SharePreferenceUtils.clear(SettingActivity.this);
                        UserAPI.saveUserInfo(userEntity);
                    }

                    @Override
                    public void onDismiss() {

                    }
                });

    }
}
